﻿Imports System.IO

Namespace Project

    Public Class ProjectFile

        Private _fullname As String

        ReadOnly Property FullName As String
            Get
                Return _fullname
            End Get
        End Property

        ReadOnly Property FileName As String
            Get
                Dim lastslash As Integer = _fullname.LastIndexOf("\")
                If lastslash >= 0 Then
                    Return _fullname.Substring(lastslash + 1, _fullname.Length - lastslash - 1)
                Else
                    Return _fullname
                End If
            End Get
        End Property

        Private _filecontents As String = ""
        Property FileContents As String
            Get
                LoadContents()
                Return _filecontents
            End Get
            Set(ByVal value As String)
                _filecontents = value
                SaveContents()
            End Set
        End Property

        Private _directory As String
        ReadOnly Property Directory As String
            Get
                Dim lastslash = _fullname.LastIndexOf("\")
                Return _fullname.Substring(0, lastslash)
            End Get
        End Property

        Private _class As Unreal.UnrealClass
        ReadOnly Property UnrealClass As Unreal.UnrealClass
            Get
                Return _class
            End Get
        End Property

        Public Sub New(ByRef fullname As String)
            _fullname = fullname
            _class = New Unreal.UnrealClass(fullname)
        End Sub

        Public Sub ParseUnrealScript()
            _class.Parse()
        End Sub

        Private Sub LoadContents()
            Dim reader As New StreamReader(_fullname)
            _filecontents = reader.ReadToEnd()
            reader.Close()
        End Sub

        Public Sub SaveContents()
            Dim writer As New StreamWriter(_fullname)
            writer.Write(_filecontents)
            writer.Close()

            MainForm.FileStatusLabel.Text = "Saved: " & FileName
        End Sub

        Public Sub OpenPath()
            Dim proc As New Process()
            Dim startinfo As New ProcessStartInfo
            startinfo.FileName = Globals.kDefaultExplorer
            startinfo.Arguments = Directory
            proc.StartInfo = startinfo
            proc.Start()
        End Sub

        ' Open an external editor
        Public Sub OpenEditor()
            ' TODO: Add in default editor
            Dim proc As New Process()
            Dim startinfo As New ProcessStartInfo
            startinfo.FileName = Globals.kDefaultEditor
            startinfo.Arguments = FullName
            proc.StartInfo = startinfo
            proc.Start()
        End Sub

    End Class
End Namespace